<?php
namespace Admin\Controller;
use Think\Controller;
class ShopController extends Controller {
	public function __construct(){
        parent::__construct();
        $this->buser = M('user');
        $user_id = session('user_id');
    }

    #产品分类
    public function shop_type(){
        $nav = M('nav')->where(array('is_show'=>1,'level'=>1))->select();
        foreach ($nav as $key => $val) {
            $nav[$key]['list'] = M('nav')->where(array('parent_id'=>$val['id']))->select();
        }
        // var_dump($nav);die;
        $this->nav = $nav;
        $this->display();
    }

    #产品列表
    public function shop_list(){
        $shop = M('product')->select();
        $this->shop = $shop;
        $this->display();
    }

    #添加产品
    public function shop_add(){
        $nav = M('nav')->where(array('level'=>1))->select();
        $brank = M('brank')->select();
        if (IS_POST) {
            
        }
        $this->nav = $nav;
        $this->brank = $brank;
        $this->display();
    }

    #ajax请求商品品牌
    public function brank()
    {
        header("Content-type: text/html; charset=utf-8");
        if (IS_AJAX) {
            $id = I('post.id');
            $res = M('nav')->where(array('id'=>$id))->select();
            echo json_encode($res);
        }
    }

    #品牌列表
    public function brank_list()
    {
        header("Content-type: text/html; charset=utf-8");
        if (IS_AJAX) {
            $id = I('post.id');
            $res = M('nav')->where(array('parent_id'=>$id))->select();
            echo json_encode($res);
        }
    }

    #商品品牌
    public function shop_brank()
    {
        $brank = M('brank')->select();
        $this->brank = $brank;
        $this->display();
    }

    #添加品牌
    public function brank_add()
    {
        $nav = M('nav')->where(array('level'=>1))->select();
        if(IS_POST){
            $data = I('post.arr');
            if ($_FILES['logo']['name'] != '') {
                $upload          = new \Think\Upload(); // 实例化上传类
                $upload->maxSize = 3145728; // 设置附件上传大小
                $upload->exts    = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
                $info            = $upload->upload();
                if (!$info) {
                    $this->error($upload->getError());
                } else {
                    $data['logo'] = '/Uploads/' . $page . $info['logo']['savepath'] . $info['logo']['savename'];
                }
            } else {
                $data['logo'] = '';
            }
            $cat_name = M('nav')->field('name')->where(array('id'=>$data['cat_id']))->find();
            $data['cat_name'] = $cat_name['name'];
            $res = M('brank')->add($data);
            if($res){
                show_msg("添加成功！");
            }else{

            }
        }
        $this->nav = $nav;
        $this->display();
    }

    #添加品牌
    public function brank_edit()
    {
        $nav = M('nav')->where(array('level'=>1))->select();
        $id = I('get.id');
        $brank = M('brank')->where(array('id'=>$id))->find();
        if(IS_POST){
            $data = I('post.arr');
            if ($_FILES['logo']['name'] != '') {
                $upload          = new \Think\Upload(); // 实例化上传类
                $upload->maxSize = 3145728; // 设置附件上传大小
                $upload->exts    = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
                $info            = $upload->upload();
                if (!$info) {
                    $this->error($upload->getError());
                } else {
                    $data['logo'] = '/Uploads/' . $page . $info['logo']['savepath'] . $info['logo']['savename'];
                }
            } else {
                $data['logo'] = '';
            }
            $cat_name = M('nav')->field('name')->where(array('id'=>$data['cat_id']))->find();
            $data['cat_name'] = $cat_name['name'];
            $res = M('brank')->where(array('id'=>$id))->save($data);
            if($res){
                show_msg("添加成功！");
            }else{

            }
        }
        $this->brank = $brank;
        $this->nav = $nav;
        $this->display();
    }

    #删除品牌
    public function brank_del()
    {
        $id = I('post.id');
        $res = M('brank')->where(array('id'=>$id))->delete();
        echo json_encode($res);
    }

    #商品评论
    public function shop_comment()
    {
        
        $this->display();
    }
}